package com.sbsj.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sbsj.mapper.LinkRoleResourceMapper;
import com.sbsj.mapper.ResourcesMapper;
import com.sbsj.models.TLinkRoleResource;
import com.sbsj.models.TLinkUserRole;
import com.sbsj.models.TResources;
import com.sbsj.models.TRole;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ResourcesService {

    private final ResourcesMapper resourcesMapper;
    private final LinkRoleResourceMapper linkRoleResourceMapper;

    public ResourcesService(ResourcesMapper resourcesMapper, LinkRoleResourceMapper linkRoleResourceMapper) {
        this.resourcesMapper = resourcesMapper;
        this.linkRoleResourceMapper = linkRoleResourceMapper;
    }

    public List<TResources> findResourcesByRoleId(String roleid)
    {
        LambdaQueryWrapper<TLinkRoleResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TLinkRoleResource::getRoleId,roleid);
        List<TLinkRoleResource> linkRoleResources = linkRoleResourceMapper.selectList(wrapper);
        List<TResources> resourcesList = new ArrayList<>();
        for(TLinkRoleResource roleResource : linkRoleResources)
        {
            TResources resources = resourcesMapper.selectById(roleResource.getResourceId());
            resourcesList.add(resources);
        }
        return resourcesList;
    }
}
